package tw.com.book;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import org.apache.commons.lang3.StringUtils;

import tw.com.book.api.task03.Task03Service;
import tw.com.book.componet.Deglagte;
import tw.com.book.componet.IssueBussinessException;
import tw.com.book.componet.Msg;
import tw.com.book.core.task03.Task03ServiceImpl;
import tw.com.book.domain.V001Type;
import tw.com.book.dto.Task003DTO;
import tw.com.book.task02.SignAccountActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseFile;
import com.parse.ParseObject;

/**
 * 登入
 * 
 * @author Andy
 * 
 */
public class UserActivity extends Activity implements Deglagte {

	private final static int SIGN_CODE = 001;

	private Task003DTO dto = new Task003DTO();
	private Task03Service service = new Task03ServiceImpl();

	private transient EditText email;
	private transient EditText password;
	private transient CheckBox remberEmail;

	private ProgressDialog loading = null;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == SIGN_CODE) {
			if (resultCode == RESULT_OK) {
				// 註冊成功
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.user_view);

		this.email = (EditText) this.findViewById(R.id.email);
		this.password = (EditText) this.findViewById(R.id.password);
		this.remberEmail = (CheckBox) this.findViewById(R.id.remberEmail);
		this.loading = new ProgressDialog(this);
		this.loading.setCancelable(true);

		final Button signingBtn = (Button) this.findViewById(R.id.signing);
		signingBtn.setOnClickListener(new SingingAction());

		final Button loggingBtn = (Button) this.findViewById(R.id.logging);
		loggingBtn.setOnClickListener(new LoggingAction());

		this.service.initValue(this.dto, this);
		this.email.setText(this.dto.getEmail());
		this.remberEmail.setChecked(dto.isRember());
	}

	/**
	 * 註冊
	 * 
	 * @author Andy
	 * 
	 */
	private class SingingAction implements OnClickListener {

		@Override
		public void onClick(View v) {
			Intent it = new Intent(UserActivity.this, SignAccountActivity.class);

			final Bundle bundle = new Bundle();
			bundle.putSerializable("dto", UserActivity.this.dto);
			it.putExtras(bundle);

			UserActivity.this.startActivityForResult(it, SIGN_CODE);
		}

	}

	/**
	 * 登入
	 * 
	 * @author Andy
	 * 
	 */
	private class LoggingAction implements OnClickListener {

		@Override
		public void onClick(View v) {
			try {
				UserActivity.this.loginAccount();
			} catch (IssueBussinessException e) {
				Toast.makeText(UserActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
				;
			}

		}
	}

	/**
	 * 註冊
	 */
	public void registerAccount() throws IssueBussinessException {

		this.service.registerAccount(dto, this);
	}

	public void loginAccount() throws IssueBussinessException {

		this.dto.setEmail(this.email.getText().toString());
		this.dto.setPassowrd(this.password.getText().toString());
		this.dto.setRember(this.remberEmail.isChecked());

		if (StringUtils.isBlank(this.dto.getEmail())) {
			throw new IssueBussinessException(//
					Msg.W001,//
					getString(R.string.email));//
		}
		if (StringUtils.isBlank(this.dto.getPassowrd())) {
			throw new IssueBussinessException(//
					Msg.W001,//
					getString(R.string.password));//
		}

		loading.setTitle(this.getString(R.string.loading));
		loading.show();

		this.service.loginAccount(dto, this);
	}

	/**
	 * 修改密碼
	 */
	public void uploadToPaser() {

		try {

			ParseObject jobAppliction = new ParseObject("jobAppliction");
			jobAppliction.put("jobname", "job");

			V001Type type = new V001Type();
			type.setCreateUser("CCC");

			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutput out = null;
			try {
				out = new ObjectOutputStream(bos);
				out.writeObject(type);

				ParseFile file = new ParseFile("file", bos.toByteArray());
				out.close();
				jobAppliction.put("file", file);
				jobAppliction.saveInBackground();

			} catch (Exception e) {

			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}

	@Override
	public void putMessage(String code, IssueBussinessException e) {
		if (StringUtils.equals(code, Task03Service.LOGGING_ACTION)) {

			if (e == null) {
				final Intent it = new Intent(this, MainActivity.class);
				this.startActivity(it);
				this.finish();
			} else {
				Toast.makeText(this, "登入失敗", Toast.LENGTH_SHORT).show();
			}
			this.loading.dismiss();
		}
		// TODO Auto-generated method stub

	}
}
